import 'package:core_package/router/basics/common.dart';
import 'package:core_package/router/basics/router.dart' as a;
import 'package:core_package/router/navigator_util.dart';
import 'package:core_package/ui/easy_refresh/error_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app_unionad/core/withdraw_deposit_manager/contract_page.dart';
import 'package:flutter_app_unionad/core/withdraw_deposit_manager/h5_generate_contract_model.dart';
import 'package:flutter_app_unionad/main/main_screen.dart';
import 'package:flutter_app_unionad/main/protocol_page.dart';
import 'package:flutter_app_unionad/model/device_info_model.dart';
import 'package:flutter_app_unionad/model/my_device_list_model.dart';
import 'package:flutter_app_unionad/model/take_save_model.dart';
import 'package:flutter_app_unionad/model/work_take_model.dart';
import 'package:flutter_app_unionad/ui/login/binding_mobile_page.dart';
import 'package:flutter_app_unionad/ui/login/change_mobile_page.dart';
import 'package:flutter_app_unionad/ui/login/change_password_page.dart';
import 'package:flutter_app_unionad/ui/login/login_for_mobile_page.dart';
import 'package:flutter_app_unionad/ui/login/login_for_password_page.dart';
import 'package:flutter_app_unionad/ui/login/register_page.dart';
import 'package:flutter_app_unionad/ui/login/web_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/cut_device_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/make_an_appointment_install_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/recharge_order_page.dart';
import 'package:flutter_app_unionad/ui/my_facility/recharge_page.dart';
import 'package:flutter_app_unionad/ui/partnership/facility_list_page.dart';
import 'package:flutter_app_unionad/ui/partnership/join_partnership_page.dart';
import 'package:flutter_app_unionad/ui/promotion/share_page.dart';
import 'package:flutter_app_unionad/ui/user_center/change_user_device_info_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item0_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item10_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item1_pages.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item2_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item3_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item4_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item5_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item6_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item7_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item8_page.dart';
import 'package:flutter_app_unionad/ui/user_center/user_center_item9_page.dart';
import 'package:flutter_app_unionad/ui/withdraw_deposit/withdraw_deposit_page.dart';
import 'package:flutter_app_unionad/ui/workbench/work_activate_page.dart';

class Routes {
  static final String initialRoute = MainScreen.routeName;

  //路由管理
  static a.Router? router;

  //配置路由
  static void configureRoutes(a.Router router) {
    //未找到 对应的route
    router.notFoundHandler = Handler(handlerFunc: (context, paramers) {
      return ErrorPage(); //报错页面
    });

    //密码登录
    router.define(LoginForPasswordPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return LoginForPasswordPage();
    }));
    //手机号码登录
    router.define(LoginForPasswordPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return LoginForMobilePage();
    }));
    //注册
    router.define(RegisterPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return RegisterPage();
    }));

    //绑定或者手机验证码登录
    router.define(BindingMobilePage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return BindingMobilePage(
        data: params == null ? null : params as Map<String, dynamic>,
      );
    }));
    //预约安装
    router.define(MakeAnAppointmentInstallPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return MakeAnAppointmentInstallPage(needPhone: params == null ? false : params as bool);
    }));
    //充值
    router.define(RechargePage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return RechargePage(infoModel: params == null ? null : params as DeviceInfoModel);
    }));
    //充值记录
    router.define(RechargeOrderPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return RechargeOrderPage(data: params);
    }));

    //用户中心item
    router.define(UserCenterItem0Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem0Page(map: params);
    }));
    router.define(UserCenterItem1Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem1Page();
    }));
    router.define(UserCenterItem2Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem2Page();
    }));
    router.define(UserCenterItem3Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem3Page();
    }));
    router.define(UserCenterItem4Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem4Page();
    }));
    router.define(UserCenterItem5Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem5Page();
    }));
    router.define(UserCenterItem6Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem6Page();
    }));
    router.define(UserCenterItem7Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem7Page();
    }));
    router.define(UserCenterItem8Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem8Page();
    }));
    router.define(UserCenterItem9Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem9Page();
    }));
    router.define(UserCenterItem10Page.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return UserCenterItem10Page();
    }));

    //加入合伙人
    router.define(JoinPartnershipPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return JoinPartnershipPage();
    }));
    //设备列表
    router.define(FacilityListPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return FacilityListPage();
    }));
    //提现
    router.define(WithdrawDepositPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return WithdrawDepositPage(accountModel: params == null ? null : params as TaskSaveModel);
    }));
    //手改手机号
    router.define(ChangeMobilePage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return ChangeMobilePage();
    }));
    //修改密码
    router.define(ChangePasswordPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return ChangePasswordPage(changePassword: params == null ? false : params as bool);
    }));

    //激活
    router.define(WorkActivatePage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return WorkActivatePage(data: params == null ? null : params as WorkTakeModel);
    }));
    //分享
    router.define(SharePage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return SharePage();
    }));
    //合约签约
    router.define(ContractPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return ContractPage(data: params == null ? null : params as H5GenerateContractModel);
    }));
    //协议
    router.define(ProtocolPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return ProtocolPage(isProtocol: params == null ? null : params as int);
    }));

    //切换设备
    router.define(CutDevicePage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return CutDevicePage(
        data: params == null ? null : params as List,
      );
    }));

    //web
    router.define(WebPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      Map map = params == null ? {} : params as Map;
      return WebPage(
        title: map['title'],
        url: map['url'],
        isSplash: map['isSplash'] == '1' ? true : false,
      );
    }));
    router.define(ChangeUserDeviceInfoPage.routeName, handler: Handler(handlerFunc: (ctx, params) {
      return ChangeUserDeviceInfoPage(params == null ? MyDeviceListModel() : params as MyDeviceListModel);
    }));
  }
}

Future gotoApp(
  BuildContext context, {
  required String route,
  bool isSplash = false,
}) async {
  if (route.length == 0) {
    return null;
  }
  Uri routeUri = Uri.parse(route);
  Map<String, String> params = {};
//Uri.queryParameters方法返回的是一个不可改动的map，所以这里创建一了个map
  params.addAll(routeUri.queryParameters);

  if (routeUri.scheme == 'nnapp') {
    String route = routeUri.host + routeUri.path;
    return NavigatorUtil.push(context, route, arguments: params);
  } else {
    params['isSplash'] = isSplash ? '1' : '0';
    return Navigator.pushNamed(
      context,
      WebPage.routeName,
      arguments: params,
    );
  }
}
